RIGHT JOIN

In this lesson, we will discuss the RIGHT JOIN keyword.

We'll cover the following

RIGHT JOIN#

The RIGHT JOIN keyword returns all records from the right table (table2), and the matched records from the left table (table1). The result is NULL from the left side when there is no match.

Syntax#

SELECT table1.column1, table2.column2...

 FROM table1

RIGHT JOIN table2

ON table1.common_field = table2.common_field;

Note: In some databases, RIGHT JOIN is called RIGHT OUTER JOIN.

Example#

Let’s say we want to return all orders and any customers that have placed an order:

Created with Fabric.js 1.6.0-rc.1 The CUSTOMERS table contains information regarding the customers, while the ORDERS tablecontains information regarding orders placed by customers. As we want the information about all orders and any customers that have paced an order, so we will use RIGHT JOIN.
1 of 3

The SQL query to retrieve all orders and some of the customers(those who have placed an order):

As you can see, the RIGHT JOIN keyword returns all records from the right table (ORDERS), even if there are no matches in the left table (CUSTOMERS).

Quick quiz!#

Q

Will the following query return the NAME and ADDRESS of the customer that ordered an item along with the items’ ORDER_ID?

SELECT CUSTOMERS.NAME, CUSTOMERS.ADDRESS ,ORDERS.ORDER_ID
FROM CUSTOMERS
RIGHT JOIN ORDERS
ON ID = CUSTOMER_ID;
A)

True

B)

False

LEFT JOIN
Exercise 1
Mark as Completed
Report an Issue